/**
 * 抽象结构，用于定义对象的类型.不提供实现
 * 行为的抽象
 */


namespace _4 {

    interface Person {
        [index: string]: string;
    }

    /***
     * 函数型接口
     * 
    */
    interface IFunc {
        (name: string): void
    }
    /***
     * 属性接口是一种描述对象属性的接口，用来规范对象的属性名、属性类型和属性值等信息
     * 属性接口
     */
    interface Person2 {
        name: string;
        // age: number;
        age?: number//可选属性
        readonly sex: number//只读属性

    }

    function greet(person: Person2) {
        console.log(`name is ${person.name} and age: ${person.age}`);
    }
    const p: Person2 = { name: "zhangsan", age: 18, sex: 1 }
    // p.sex = 1
    greet(p)


    // 👇️ const p1: Person
    const p1: Person = { name: 'Tom', country: 'Chile' };

    interface Person3 {
        name: string;
        age: number;
        greet(name: string): void;
    }
    /****
     * 
     * 类接口。接口继承接口
     */
    class Student2 implements Person3 {
        constructor(public name: string, public age: number) { }

        greet(name: string) {
            console.log(`name, ${name},  ${this.name}.`);
        }
    }
    interface Speakable {
        speak(): void;
    }
    interface Eatable {
        eat(): void;
    }
    class Person4 implements Speakable, Eatable {
        speak(): void {
            throw new Error("Method not implemented.");
        }
        eat(): void {
            throw new Error("Method not implemented.");
        }
    }

}
namespace e {
    class Person {
        constructor(public name: string) { }
    }
    interface WithNameClass {
        // 修饰普通函数不用加new，修饰构造函数要加new
        new(name: string): any;
    }
    function created(cla: WithNameClass, name: string): void {
        return new cla(name);
    }
    let res = created(Person, '张三');
    console.log(res); // Person { name: '张三' }
}


